<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_utils_Picture
{
	public static function getProportionalSizeByMaxSize($imgSrc, $dstMaxWidth, $dstMaxHeight) 
	{
		list($srcWidth, $srcHeight) = GetImageSize($imgSrc); 
        $ratioHeight = round(($dstMaxWidth / $srcWidth) * $srcHeight);
        $ratioWidth = round(($dstMaxHeight / $srcHeight) * $srcWidth);

		if($srcWidth <= $dstMaxWidth && $srcHeight <= $dstMaxHeight)
		{
			$dstMaxWidth = $srcWidth;
			$dstMaxHeight = $srcHeight;
		}
		else 
		{
			if ($srcWidth <= $srcHeight) 
			{
                $cMax = $dstMaxWidth;
				$dstMaxWidth = $ratioWidth;
                if($dstMaxWidth > $cMax)
                {
                    $dstMaxWidth = $cMax;
                    $dstMaxHeight = $ratioHeight;
                }
			} 
			else 
			{
                $cMax = $dstMaxHeight;
				$dstMaxHeight = $ratioHeight;
                if($dstMaxHeight>$cMax)
                {
                    $dstMaxHeight = $cMax;
                    $dstMaxWidth = $ratioWidth;
                }
			}
		}

		$result["width"] = $dstMaxWidth;
		$result["height"] = $dstMaxHeight;
		return $result;
	}
	
	public static function createThumb($imgSrc, $imgDest, $destWidth, $destHeight, $quality = 80) 
	{
		if(!file_exists($imgSrc))
		{
			return false;
		}
		
		list($srcWidth, $srcHeight, $type) = GetImageSize($imgSrc);  

		if($srcWidth < $destWidth && $srcHeight < $destHeight)
		{
			$destWidth = $srcWidth;
			$destHeight = $srcHeight;
		}
		else
		{
			$ratioHeight = round(($destWidth / $srcWidth) * $srcHeight);
			$ratioWidth = round(($destHeight / $srcHeight) * $srcWidth);
			
			if($ratioHeight > $destHeight) 
			{
				$destWidth = $ratioWidth;
			}
			else 
			{
				$destHeight = $ratioHeight;
			}
		}

        switch ($type)
        {
            case 1: $fromPic = imagecreatefromgif($imgSrc); break;
            case 2: $fromPic = imagecreatefromjpeg($imgSrc);  break;
            case 3: $fromPic = imagecreatefrompng($imgSrc); break;
            default: return false;  break;
        }

        if($fromPic !== false)
        {
            if (function_exists('ImageCreateTrueColor') == true)
            {
                $newPic = ImageCreateTrueColor($destWidth,$destHeight);
            }
            else
            {
                $newPic = ImageCreate($destWidth,$destHeight);
            }

            if($type == 3)
            {
                imagealphablending($newPic, false);
                imagesavealpha($newPic,true);
                $transparent = imagecolorallocatealpha($newPic, 255, 255, 255, 127);
                imagefill($newPic, 0, 0, $transparent);
            }

            if (function_exists('ImageCopyResampled'))
            {
                ImageCopyResampled($newPic, $fromPic, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
            }
            else
            {
                ImageCopyResized($newPic, $fromPic, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
            }

            imagejpeg($newPic, $imgDest, $quality);
            ImageDestroy($newPic);
            ImageDestroy($fromPic);
        }
		
		if(file_exists($imgDest))
		{
			return true;
		}

		return false;
	}	
}